package task

import (
	"cvevulner/common"
	"cvevulner/taskhandler"
	"errors"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/config"
	"github.com/astaxie/beego/logs"
)

// GetYamlData Get yaml data source
func GetYamlData() error {
	defer common.Catchs()
	logs.Info("Get the yaml data source task started")
	// Query the cve to be processed, 1: add; 2: modify
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("GetYamlData, config init, error:", err)
		return err
	}
	apiUrl := BConfig.String("yaml::apiurl")
	if apiUrl == "" {
		logs.Error("GetYamlData, config yaml::apiurl, error: invalid value")
		return errors.New("value is nil")
	}
	// Get the data source of the table
	_, err = taskhandler.GetYamlTables(apiUrl)
	// Get yaml
	if err == nil {
		_, err = taskhandler.GetYamlByGit(apiUrl)
	}
	// Synchronize other sources of yaml version
	taskhandler.SyncEulerYaml()
	// Delete historical yaml source version data that does not exist
	taskhandler.DelHistoryEulerYaml()
	logs.Info("End of the task of obtaining yaml data source")
	return err
}

// GetEulerYamlData Get yaml data source
func GetEulerYamlData() error {
	defer common.Catchs()
	logs.Info("Get the euleryaml data source task started")
	// Query the cve to be processed, 1: add; 2: modify
	eulerUrl := beego.AppConfig.String("yaml::eulerurl")
	if eulerUrl == "" {
		logs.Error("GetEulerYamlData, config yaml::eulerUrl, error: invalid value")
		return errors.New("eulerUrl value is nil")
	}
	_, err := taskhandler.GetEulerYamlInfo(eulerUrl)
	// Synchronize other sources of yaml version
	taskhandler.SyncEulerYaml()
	// Delete historical yaml source version data that does not exist
	taskhandler.DelHistoryEulerYaml()
	logs.Info("End of the task of obtaining euleryaml data source")
	return err
}
